<?php
class Paging {

	private function __prepareQueryString($queryString) {
		$temp = explode("&", $queryString);
		if(!$temp) return "";
		$output = array();
		foreach ($temp as $str) {
			if(strpos($str, "fr=") === FALSE) $output[] = $str;
		}
		return implode("&", $output);
	}
	
	public static function numberPaging($totalRecords, $fr, $pagingSize) {
        
		$url = $_SERVER['PHP_SELF']."?".self::__prepareQueryString($_SERVER['QUERY_STRING']);
		if($totalRecords <= $pagingSize) return false;

		$totalPages		= ceil($totalRecords/$pagingSize);
		$start			= 1;
		$end			= $numOfLinks = 10;
		$half			= ($numOfLinks / 2);
		$curent_page	= ($fr / $pagingSize) + $start;
		if ($numOfLinks > $totalPages ) $numOfLinks = $totalPages;
		if ($fr && $curent_page >= $half) {
			$start	= (($fr / $pagingSize) + $start) - ($half);
			$end	= ($fr / $pagingSize) + $numOfLinks - ($half -1);
			$pLoop = ($fr / $pagingSize);

		}
		if (($start + $numOfLinks) > $totalPages && $totalPages >= $numOfLinks) {
			$start	= $totalPages - $numOfLinks + 1;
			$end	= $totalPages;
			$pLoop = $totalPages - $numOfLinks;
		}
		$start = ($start < 1) ? 1 : $start;
		$pre   = $fr-$pagingSize;
		$next  = $fr+$pagingSize;

		?>
		<style>
		.Paginator { font-family: Arial, Helvetica, sans-serif;font-size: 12px; padding-top: 10px; padding-bottom: 10px; margin-left: 0px; margin-right: auto;}
.Paginator a, .pageList .this-page { padding: 2px 6px; border: solid 1px #ddd; background: #fff; text-decoration: none; }
.Paginator a:visited { padding: 2px 6px; border: solid 1px #ddd; background: #fff; text-decoration: none; }
.Paginator .this-page {padding: 2px 6px; border-color: #999; font-weight: bold; font-size: 13px; vertical-align: top; background: #fff; color: #FF0084; }
.Paginator a:hover {color: #fff; background: #0063DC; border-color: #036; text-decoration: none;}
		</style>
		<?php									
		echo "<div class='Paginator'>\n";
		if($fr>1)
		{
			echo "<a href='$url&fr=$pre'>Prev</a>\n";
		}

		for ($i=$start; $i<=$end; $i++){
			$p_no = (($i - 1) * $pagingSize);
			if ($fr!=$p_no){
				echo "<a href='$url&fr=$p_no'>$i</a>\n";
			}else{
				echo "<span class='this-page'>$i</span>\n";
			}
		}
		if($fr <($totalRecords-$pagingSize))
		{
			echo "<a href='$url&fr=$next'>Next</a>";
		}
		echo "</div>";
	}
}
?>